package com.orangelife.controller;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
import javax.ws.rs.ext.ExceptionMapper;

import org.apache.commons.httpclient.HttpStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.orangelife.setting.constant.CodeConst;
import com.orangelife.setting.constant.InfoConst;

public class InvokeFaultExceptionMapper implements
		ExceptionMapper<WebApplicationException> {

	/* log initation */
	Logger logger = LoggerFactory.getLogger(InvokeFaultExceptionMapper.class);

	@Override
	public Response toResponse(WebApplicationException ex) {
		logger.error(InfoConst.SYS_INFO_3001, ex);

		Map<String, Object> resp = new HashMap<String, Object>();
		resp.put("errCode", CodeConst.SYS_CODE_3001);
		resp.put(
				"errInfo",
				String.format("%s:%s", ex.getResponse().getStatus(), ex
						.getResponse().getStatusInfo().getReasonPhrase()));

		ResponseBuilder rb = Response.status(HttpStatus.SC_OK);
		rb.type("application/json;charset=UTF-8");
		rb.language(Locale.SIMPLIFIED_CHINESE);
		rb.entity(resp);
		return rb.build();
	}

}
